﻿/*
	Copyright © Bogdan Kobets 2011
	http://ElasticLogic.com
*/

using System.Collections.Generic;
using ElasticLogic.TagsMarker.Concepts;

namespace ElasticLogic.TagsMarker.Collections
{

	public sealed class TagsCollection<E> : LabelsCollection<Tag<E>>
		where E : class
	{
		public TagCreator<E> Creator { get; set; }

		public TagsCollection()
			: base()
		{
		}

		public List<Tag<E>> Categorized
		{
			get { return Marker<E>.GetAssignmentTags(true); }
		}

		public List<Tag<E>> Uncategorized
		{
			get { return Marker<E>.GetAssignmentTags(false); }
		}

		public List<Tag<E>> Used
		{
			get { return Marker<E>.GetUsingTags(true); }
		}

		public List<Tag<E>> Unused
		{
			get { return Marker<E>.GetUsingTags(false); }
		}

		public bool Add(string caption)
		{
			if (Creator == null)
				return false;

			Tag<E> tag = Creator(caption);

			if (tag != null)
				return base.Add(tag);
			else
				return false;
		}

		public bool Remove(string caption)
		{
			return this.Remove(base[caption]);
		}

		public bool Remove(Tag<E> tag)
		{
			if (base.Contains(tag))
			{
				Marker<E>.ExcludeTagFromCategory(tag);
				Marker<E>.ClearTagFromEntities(tag);
				return base.RemoveItem(tag);
			}
			else
				return false;
		}

		public void Clear()
		{
			foreach (Tag<E> tag in base.Items)
			{
				this.Remove(tag);
			}
		}

		public List<Tag<E>> ByCategory(Category<E> category)
		{
			return Marker<E>.GetCategoryTags(category);
		}

		public List<Tag<E>> ByEntity(E entity)
		{
			return Marker<E>.GetEntityTags(entity);
		}

	}

	public delegate Tag<E> TagCreator<E>(string caption)
		where E:class;

}
